package cc.rengu.igas.opms.core.service.trans.apppay;

import cc.rengu.igas.opms.common.constant.OpmsParamConstant;
import cc.rengu.igas.opms.common.enums.RespCodeEnum;
import cc.rengu.igas.opms.core.service.base.ManageService;
import cc.rengu.igas.opms.facade.request.EbankAccountInfoQueryRequest;
import cc.rengu.igas.opms.facade.response.EbankAccountInfoQueryResponse;
import cc.rengu.igas.share.common.util.GlobalEsbUtil;
import cc.rengu.igas.smps.facade.request.BankAcctInfoQueryRequest;
import cc.rengu.igas.smps.facade.response.BankAcctInfoQueryResponse;
import cc.rengu.igas.smps.facade.result.Result;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.dao.SignAcctInfoMapper;
import cc.rengu.oltp.service.common.dao.impl.SignAcctInfoMapperImpl;
import cc.rengu.oltp.service.common.entity.BinInfo;
import cc.rengu.oltp.service.common.entity.ChannelCallCfg;
import cc.rengu.oltp.service.common.entity.SignAcctInfo;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.common.enums.CheckResultEnum;
import cc.rengu.oltp.service.common.enums.TransStatusEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.model.OrderInfo;
import cc.rengu.oltp.service.realize.BinInfoService;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.service.realize.TransCustomImpl;
import cc.rengu.oltp.service.realize.impl.BinInfoServiceImpl;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.service.realize.impl.SysParamServiceImpl;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

import java.util.List;

/**
 * 查询账户信息
 * 收银台—>支付通道—>ESB
 * T00PM056
 *
 * @author fuyp
 * @date 2020/7/10.
 */
public class EbankAccountInfoQueryService extends ManageService implements TransCustomImpl {
    @Override
    protected Object bizBeforeCust() throws Exception {
        EbankAccountInfoQueryRequest ebankAccountInfoQueryRequest = new EbankAccountInfoQueryRequest();
        ConvertUtil.convertOutput(ebankAccountInfoQueryRequest);
        return ebankAccountInfoQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();

        //获取请求数据
        EbankAccountInfoQueryRequest ebankAccountInfoQueryRequest = (EbankAccountInfoQueryRequest) request;
        EbankAccountInfoQueryResponse ebankAccountInfoQueryResponse = new EbankAccountInfoQueryResponse();
        String instId = ebankAccountInfoQueryRequest.getHeader().getInstId();

        SysParamService sysParamService = new SysParamServiceImpl();
        SysParam sysParam = sysParamService.getSysParamInfo(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID), OpmsParamConstant.OPMS_SYS_PARAM, OpmsParamConstant.CORPORATION_ORG_CODE);
        if (null == sysParam || null == sysParam.getParamStatus()) {
            rglog.info("法人行机构编码未配置或配置失效,instId:<{}>", instId);
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }

        /*获取卡bin信息*/
        BinInfoService binInfoService = new BinInfoServiceImpl();
        BinInfo binInfo = binInfoService.getCardBinInfo(ebankAccountInfoQueryRequest.getAcctNo());

        /*if (null == binInfo ) {
            rglog.error("获取卡BIN信息失败");
            throw new BizException(RespCodeEnum.GET_CARD_BIN_INFO_ERROR.getRespCode(), RespCodeEnum.GET_CARD_BIN_INFO_ERROR.getRespDesc());
        }*/

        /*获取卡类型  借贷记*/
        String cardType = binInfo == null ? "" : binInfo.getCardType();

        cc.rengu.igas.smps.facade.base.Header header = new cc.rengu.igas.smps.facade.base.Header();
        DubboService dubboService = new DubboServiceImpl();

        if (binInfo != null && sysParam.getParamValue().contains(binInfo.getIssuerCode().substring(0, 4))) {
            switch (cardType) {
                /*借记卡*/
                case "1":
                    /*准备请求报文 调起核心esb接口*/
                    BankAcctInfoQueryRequest coreAcctInfoQueryRequest = new BankAcctInfoQueryRequest();

                    /* 设置Header */
                    BeanUtil.beanCopy(ebankAccountInfoQueryRequest.getHeader(), header);
                    header.setVersion("1.0.0");
                    header.setTxnNum("00010000200000");
                    header.setTraceNo(GlobalEsbUtil.generateEsbSeqeuenceNumber(ebankAccountInfoQueryRequest.getHeader().getInstId()));
                    coreAcctInfoQueryRequest.setHeader(header);
                    coreAcctInfoQueryRequest.setAcctNo(ebankAccountInfoQueryRequest.getAcctNo()); //账号

                    /* 调起esb接口 */
                    cc.rengu.igas.smps.facade.result.Result result = (cc.rengu.igas.smps.facade.result.Result)
                            dubboService.callDubboService(OpmsParamConstant.DUBBO_BANK_SUP_SERVICE, "bankAcctInfoQuery", coreAcctInfoQueryRequest);

                    //校验调用esb状态
                    if (null == result || !result.isSuccess()) {
                        rglog.error("调用核心查询卡信息，服务异常！");
                        throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
                    }
                    BankAcctInfoQueryResponse coreAcctInfoQueryResponse = (BankAcctInfoQueryResponse) result.getResult();
                    if (OpmsParamConstant.DUBBO_OVERTIME.equals(coreAcctInfoQueryResponse.getRespCode())) {
                        rglog.info("调用核心查询卡信息DUBBO接口-超时，respCode:<{}>,respDesc:<{}>", coreAcctInfoQueryResponse.getRespCode(), coreAcctInfoQueryResponse.getRespDesc());
                        throw new BizException(RespCodeEnum.BIZ_TIMEOUT_ERROR.getRespCode(), RespCodeEnum.BIZ_TIMEOUT_ERROR.getRespDesc());
                    } else if (!OpmsParamConstant.DUBBO_SUCCESS.equals(coreAcctInfoQueryResponse.getRespCode())) {
                        rglog.info("调用核心查询卡信息DUBBO接口-失败，respCode:<{}>,respDesc:<{}>", coreAcctInfoQueryResponse.getRespCode(), coreAcctInfoQueryResponse.getRespDesc());
                        throw new BizException(coreAcctInfoQueryResponse.getRespCode(), coreAcctInfoQueryResponse.getRespDesc());
                    } else {
                        rglog.info("调用核心查询卡信息DUBBO接口-成功，respCode:<{}>,respDesc:<{}>", coreAcctInfoQueryResponse.getRespCode(), coreAcctInfoQueryResponse.getRespDesc());
                    }

                    // 核心返回数据
                    ebankAccountInfoQueryResponse.setAcctNo(coreAcctInfoQueryRequest.getAcctNo()); //账号
                    ebankAccountInfoQueryResponse.setCertNo(coreAcctInfoQueryResponse.getCertNo()); //证件号码
                    ebankAccountInfoQueryResponse.setCardHoldName(coreAcctInfoQueryResponse.getCustomerName()); //持卡人
//                    ebankAccountInfoQueryResponse.setPhoneNo(""); // 手机号

                    String coreCertType = coreAcctInfoQueryResponse.getCertType();
                    if ("A".equals(coreCertType)) { //身份证 - 01
                        coreCertType = "01";
                    } else if ("B".equals(coreCertType)) { //军官证 - 02
                        coreCertType = "02";
                    } else if ("G".equals(coreCertType)) { //港澳通行证 - 07
                        coreCertType = "07";
                    } else if ("M".equals(coreCertType)) { //户口本 - 04
                        coreCertType = "04";
                    } else if ("J".equals(coreCertType)) { //护照 - 03
                        coreCertType = "03";
                    } else if ("Z".equals(coreCertType)) { //其他 - 11
                        coreCertType = "11";
                    }
                    ebankAccountInfoQueryResponse.setCertType(coreCertType); //证件类型

                    String acctType = coreAcctInfoQueryResponse.getAcctGroup();
                    if ("CR".equals(acctType)) { //活期卡 - 02
                        acctType = "02";
                    } else if ("LN".equals(acctType)) { //贷款账户 -05
                        acctType = "05";
                    } else if ("SA".equals(acctType)) { //活期折 -01
                        acctType = "01";
                    } else if ("TD".equals(acctType)) { //定期账户 -07
                        acctType = "07";
                    }
//                    else if ("GL".equals(acctType)) { //内部账
//                        acctType =  OpmsParamConstant.STR_ONE;
//                    }
                    ebankAccountInfoQueryResponse.setAcctType(acctType); //账号类型

                    break;
                /*贷记卡*/
                case "2":
                    /*准备请求报文 调起银树esb接口*/
                    //TODO 本行贷记卡通过卡号查询卡信息

                    /* 调起esb接口 */
                    cc.rengu.igas.smps.facade.result.Result coreAcctInfoResult = new Result();

                    //校验调用esb状态
                    if (null == coreAcctInfoResult || !coreAcctInfoResult.isSuccess()) {
                        rglog.error("调用银树查询卡信息，服务异常！");
                        throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
                    }
                    if (OpmsParamConstant.DUBBO_OVERTIME.equals("")) {
                        rglog.info("调用银树查询卡信息DUBBO接口-超时，respCode:<{}>,respDesc:<{}>", "", "");
                        throw new BizException(RespCodeEnum.BIZ_TIMEOUT_ERROR.getRespCode(), RespCodeEnum.BIZ_TIMEOUT_ERROR.getRespDesc());
                    } else if (!OpmsParamConstant.DUBBO_SUCCESS.equals("")) {
                        rglog.info("调用银树查询卡信息DUBBO接口-失败，respCode:<{}>,respDesc:<{}>", "", "");
                        throw new BizException("", "");
                    } else {
                        rglog.info("调用银树查询卡信息DUBBO接口-成功，respCode:<{}>,respDesc:<{}>", "", "");
                    }

                    //银树返回数据
//                    ebankAccountInfoQueryResponse.setAcctNo(); //账号
////                    ebankAccountInfoQueryResponse.setCertNo(); //证件号码
////                    ebankAccountInfoQueryResponse.setCardHoldName(); //持卡人
////                    ebankAccountInfoQueryResponse.setAcctType(OpmsParamConstant.STR_SIX); //账号类型 - 公务卡06
//                    ebankAccountInfoQueryResponse.setPhoneNo(""); //手机号

                    String certType = "";
                    if ("A".equals(certType)) { //身份证 - 01
                        certType = "01";
                    } else if ("B".equals(certType)) { //军官证 - 02
                        certType = "02";
                    } else if ("G".equals(certType)) { //港澳通行证 - 07
                        certType = "07";
                    } else if ("M".equals(certType)) { //户口本 - 04
                        certType = "04";
                    } else if ("J".equals(certType)) { //护照 - 03
                        certType = "03";
                    } else if ("Z".equals(certType)) { //其他 - 11
                        certType = "11";
                    }

                    ebankAccountInfoQueryResponse.setCertType(certType); //证件类型
                    break;
            }
        }
        /*他行卡 - 已存在签约信息*/
        else {
            // 根据渠道&卡号获取用户号
            SignAcctInfoMapper signAcctInfoMapper = new SignAcctInfoMapperImpl();
            List<SignAcctInfo> signAcctInfos = signAcctInfoMapper.selectSignAcctInfoBySignAcctNo(
                    ebankAccountInfoQueryRequest.getHeader().getInstId(),//行号
                    ebankAccountInfoQueryRequest.getAcctNo());//账号


            if (null != signAcctInfos && !signAcctInfos.isEmpty()) {
                //返回数据
                SignAcctInfo signAcctInfo = signAcctInfos.get(0);
                ebankAccountInfoQueryResponse.setAcctNo(signAcctInfo.getSignAcctNo()); //账号
                ebankAccountInfoQueryResponse.setAcctType(signAcctInfo.getSignAcctType()); //账号类型
                ebankAccountInfoQueryResponse.setCertType(signAcctInfo.getCertType()); //证件类型
                ebankAccountInfoQueryResponse.setCertNo(signAcctInfo.getCertNo()); //证件号码
                ebankAccountInfoQueryResponse.setPhoneNo(signAcctInfo.getReserveMobileNo()); //手机号
                ebankAccountInfoQueryResponse.setCardHoldName(signAcctInfo.getCardHoldName()); //持卡人

            } else {
                /*准备请求报文 调起核心esb接口*/
                BankAcctInfoQueryRequest coreAcctInfoQueryRequest = new BankAcctInfoQueryRequest();

                /* 设置Header */
                BeanUtil.beanCopy(ebankAccountInfoQueryRequest.getHeader(), header);
                header.setVersion("1.0.0");
                header.setTxnNum("00010000200000");
                header.setTraceNo(GlobalEsbUtil.generateEsbSeqeuenceNumber(ebankAccountInfoQueryRequest.getHeader().getInstId()));
                coreAcctInfoQueryRequest.setHeader(header);
                coreAcctInfoQueryRequest.setAcctNo(ebankAccountInfoQueryRequest.getAcctNo()); //账号

                /* 调起esb接口 */
                cc.rengu.igas.smps.facade.result.Result result = (cc.rengu.igas.smps.facade.result.Result)
                        dubboService.callDubboService(OpmsParamConstant.DUBBO_BANK_SUP_SERVICE, "bankAcctInfoQuery", coreAcctInfoQueryRequest);

                //校验调用esb状态
                if (null == result || !result.isSuccess()) {
                    rglog.error("调用核心查询卡信息，服务异常！");
                    throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
                }
                BankAcctInfoQueryResponse coreAcctInfoQueryResponse = (BankAcctInfoQueryResponse) result.getResult();
                if (OpmsParamConstant.DUBBO_OVERTIME.equals(coreAcctInfoQueryResponse.getRespCode())) {
                    rglog.info("调用核心查询卡信息DUBBO接口-超时，respCode:<{}>,respDesc:<{}>", coreAcctInfoQueryResponse.getRespCode(), coreAcctInfoQueryResponse.getRespDesc());
                    throw new BizException(RespCodeEnum.BIZ_TIMEOUT_ERROR.getRespCode(), RespCodeEnum.BIZ_TIMEOUT_ERROR.getRespDesc());
                } else if (!OpmsParamConstant.DUBBO_SUCCESS.equals(coreAcctInfoQueryResponse.getRespCode())) {
                    rglog.info("调用核心查询卡信息DUBBO接口-失败，respCode:<{}>,respDesc:<{}>", coreAcctInfoQueryResponse.getRespCode(), coreAcctInfoQueryResponse.getRespDesc());
                    throw new BizException(coreAcctInfoQueryResponse.getRespCode(), coreAcctInfoQueryResponse.getRespDesc());
                } else {
                    rglog.info("调用核心查询卡信息DUBBO接口-成功，respCode:<{}>,respDesc:<{}>", coreAcctInfoQueryResponse.getRespCode(), coreAcctInfoQueryResponse.getRespDesc());
                }

                // 核心返回数据
                ebankAccountInfoQueryResponse.setAcctNo(coreAcctInfoQueryRequest.getAcctNo()); //账号
                ebankAccountInfoQueryResponse.setCertNo(coreAcctInfoQueryResponse.getCertNo()); //证件号码
                ebankAccountInfoQueryResponse.setCardHoldName(coreAcctInfoQueryResponse.getCustomerName()); //持卡人
//                    ebankAccountInfoQueryResponse.setPhoneNo(""); // 手机号

                String coreCertType = coreAcctInfoQueryResponse.getCertType();
                if ("A".equals(coreCertType)) { //身份证 - 01
                    coreCertType = "01";
                } else if ("B".equals(coreCertType)) { //军官证 - 02
                    coreCertType = "02";
                } else if ("G".equals(coreCertType)) { //港澳通行证 - 07
                    coreCertType = "07";
                } else if ("M".equals(coreCertType)) { //户口本 - 04
                    coreCertType = "04";
                } else if ("J".equals(coreCertType)) { //护照 - 03
                    coreCertType = "03";
                } else if ("Z".equals(coreCertType)) { //其他 - 11
                    coreCertType = "11";
                }
                ebankAccountInfoQueryResponse.setCertType(coreCertType); //证件类型

                String acctType = coreAcctInfoQueryResponse.getAcctGroup();
                if ("CR".equals(acctType)) { //活期卡 - 02
                    acctType = "02";
                } else if ("LN".equals(acctType)) { //贷款账户 -05
                    acctType = "05";
                } else if ("SA".equals(acctType)) { //活期折 -01
                    acctType = "01";
                } else if ("TD".equals(acctType)) { //定期账户 -07
                    acctType = "07";
                }
//                    else if ("GL".equals(acctType)) { //内部账
//                        acctType =  OpmsParamConstant.STR_ONE;
//                    }
                ebankAccountInfoQueryResponse.setAcctType(acctType); //账号类型
            }
        }

        //组装响应
        ebankAccountInfoQueryResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        ebankAccountInfoQueryResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

        BizResponse<EbankAccountInfoQueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(ebankAccountInfoQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        EbankAccountInfoQueryResponse ebankAccountInfoQueryResponse = (EbankAccountInfoQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(ebankAccountInfoQueryResponse);
    }

    @Override
    public ChannelCallCfg channelCallSceneDiscern(Object request, List<ChannelCallCfg> channelCallCfgList) throws Exception {
        return null;
    }

    @Override
    public CheckResultEnum channelCallPreCheck(Object request, ChannelCallCfg channelCallCfg, OrderInfo orderInfo) throws Exception {
        return null;
    }

    @Override
    public TransStatusEnum channelCallResultProc(Object request, ChannelCallCfg channelCallCfg, TransStatusEnum transStatusEnum, Object channelResponse, OrderInfo needUpdateOrderInfo) throws Exception {
        return null;
    }
}